<?php
class TeacherSystemAction extends Action {

    public function _initialize() {
        $Config = A('Config');
        $Config->checkLogIn();
        $Config->checkUserLevel(array('3'));
    }
    
    // 教师登陆
    public function login() {
        // 传配置
        $Setting = M('Setting');
        $setting = $Setting->find(1);
        
        // $User = M('User');
        // $user = $User->join(array('econ_user_type ON econ_user.user_type_id = econ_user_type.user_type_id'))->find();
        
        $this->assign('site_name', $setting['site_name']);
        
        // 判断是否已经登陆
        if (isset($_SESSION['user']) && in_array($_SESSION['user']['user_type_id'], array('3'))) {
            $this->assign('jumpUrl', __APP__);
            $this->success('您已登陆成功！');
        }
        
        if (isset($_POST['submitted'])) {
        
            if($_SESSION['verify'] != md5($_POST['verify'])) {
                $this->error('验证码错误！');
            }
        
            $user_data = array();
            $user_data['user_name'] = $_POST['user_name'];
            $user_data['password'] = sha1($_POST['password']);
            
            $User = M('User');
            if ($user = $User->join(array('econ_user_type ON econ_user.user_type_id = econ_user_type.user_type_id'))->where($user_data)->find()) {
                if (!in_array($user['user_type_id'], array('3'))) {
                    $this->error('您不是教师用户！');
                }
                $_SESSION['user'] = $user;
                $this->assign('jumpUrl', __APP__);
                $this->success('登陆成功，现在为您转向后台管理！');
            } else {
                $this->error('用户名或者密码错误！');
            }
        }
        $this->display();
    }
    
    // 教师文章
    public function article() {
        $Article = M('Article');
        import("ORG.Util.Page");
        
        $count = $Article->where(array('user_id' => $_SESSION['user']['user_id']))->count();
        $Page = new Page($count, 25);
        $article = $Article->where(array('user_id' => $_SESSION['user']['user_id']))->order(array('add_time' => 'desc'))->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $this->assign('article', $article);
        
        $show = $Page->show();
        $this->assign('page',$show);
        
        $cat_id = 29;                                   // 教师文章
        $this->assign('cat_id', $cat_id);
        $this->display('Article:index');
    }
    
    // 教师共享文件
    public function file() {
        $File = M('File');
        import("ORG.Util.Page");
        $count = $File->where(array('user_id' => $_SESSION['user']['user_id']))->count();
        $Page = new Page($count, 25);
        $show = $Page->show();
        $this->assign('page', $show);
        
        $file = $File->where(array('user_id' => $_SESSION['user']['user_id']))->order(array('file_id' => 'asc'))->join(array('econ_category ON econ_file.cat_id = econ_category.cat_id'))->limit($Page->firstRow . ',' . $Page->listRows)->select();
        
        $this->assign('file', $file);
        $this->display('file:index');
    }

}
?>